package com.qdxwx.data.service.user;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.data.mapper.user.UserDownloadMapper;
import com.qdxwx.models.course.CourseFile;
import com.qdxwx.models.document.Document;
import com.qdxwx.models.enums.ModuleType;
import com.qdxwx.models.picture.Picture;
import com.qdxwx.models.user.User;
import com.qdxwx.models.user.UserCount;
import com.qdxwx.models.user.UserDownload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class UserDownloadServiceImpl implements UserDownloadService {

    @Autowired
    private UserDownloadMapper userDownloadMapper;
    @Autowired
    private UserCountService userCountService;

    @Override
    public List<UserDownload> getList(UserDownload query, PageRequest pageRequest) {
        if (pageRequest == null) {
            pageRequest = new PageRequest(0);
        }
        return userDownloadMapper.selectListPage(query, pageRequest);
    }

    @Override
    public UserDownload get(long id) {
        return userDownloadMapper.selectById(id);
    }

    @Override
    public int count(UserDownload query) {
        return userDownloadMapper.count(query);
    }

    @Override
    public int insert(UserDownload userDownload) {
        if (userDownload == null) return 0;
        userDownload.setCreateTime(LocalDateTime.now());
        if (userDownloadMapper.insert(userDownload) > 0) {
            UserCount userCount = new UserCount();
            userCount.setId(userDownload.getUserId());
            if (userDownload.getTypeId() == ModuleType.DOCUMENT.ordinal()) {
                userCount.setDocumentDownDay(1);
            } else if (userDownload.getTypeId() == ModuleType.PICTURE.ordinal()) {
                userCount.setPictureDownDay(1);
            } else {
                return 1;
            }
            userCountService.append(userCount);
            return 1;
        }
        return 0;
    }

    @Override
    public int create(Picture picture, User user) {
        //下载记录增加一条
        if (user == null || picture == null) {
            return 0;
        }
        UserDownload log = new UserDownload();
        log.setTitle(picture.getTitle());
        log.setUserId(user.getUserId());
        log.setUserName(user.getUserName());
        log.setInfoId(picture.getId());
        log.setTypeId(ModuleType.PICTURE.ordinal());
        log.setPrice(picture.getPrice());
        return insert(log);
    }

    @Override
    public int create(Document document, User user) {
        //下载记录增加一条
        if (user == null || document == null) {
            return 0;
        }
        UserDownload log = new UserDownload();
        log.setTitle(document.getTitle());
        log.setUserId(user.getUserId());
        log.setUserName(user.getUserName());
        log.setInfoId(document.getId());
        log.setTypeId(ModuleType.DOCUMENT.ordinal());
        log.setPrice(document.getPrice());
        return insert(log);
    }

    @Override
    public int create(CourseFile courseFile, User user) {
        //下载记录增加一条
        if (user == null || courseFile == null) {
            return 0;
        }
        UserDownload log = new UserDownload();
        log.setTitle(courseFile.getTitle());
        log.setUserId(user.getUserId());
        log.setUserName(user.getUserName());
        log.setInfoId(courseFile.getId());
        log.setTypeId(ModuleType.COURSE.ordinal());
        log.setPrice(0);
        return insert(log);
    }

    @Override
    public int delete(long id) {
        return userDownloadMapper.delete(id);
    }

    @Override
    public int updateCombine(long userId1, long userId2) {
        return userDownloadMapper.updateCombine(userId1,userId2);
    }
}
